package com.tanhua.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.interceptor.UserHolder;
import model.domain.Question;
import model.domain.Settings;
import model.domain.UserInfo;
import model.vo.PageResult;
import model.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    public SettingsVo settings() {
        SettingsVo settingsVo = new SettingsVo();
        Long userId = UserHolder.getUserId();
        settingsVo.setId(userId);

        String mobile = UserHolder.getMobile();
        settingsVo.setPhone(mobile);

        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你爱我我爱你?" : question.getTxt();
        settingsVo.setStrangerQuestion(txt);

        Settings settings = settingsApi.findByUserId(userId);
        if (settings != null) {
            BeanUtils.copyProperties(settings, settingsVo);
        }

        return settingsVo;
    }

    public void saveQuestion(String content) {
        Long userId = UserHolder.getUserId();
        Question question = questionApi.findByUserId(userId);
        if (question != null) {
            question.setTxt(content);
            questionApi.update(question);
        } else {
            /*questionApi.save(new Question(){{
                setId(userId);
                setTxt(content);
            }});*/
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }
    }

    public void saveSetting(Map map) {
        boolean likeNotification = (Boolean) map.get("likeNotification");
        boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");
        Long userId = UserHolder.getUserId();

        Settings settings = settingsApi.findByUserId(userId);
        if (settings != null) {
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.update(settings);
        } else {
            settingsApi.save(new Settings(null, userId, likeNotification, pinglunNotification, gonggaoNotification));
        }
    }

    public PageResult blackList(int page, int pageSize) {
        Long userId = UserHolder.getUserId();

        IPage<UserInfo> blackList = blackListApi.findByUserId(userId, page, pageSize);

        return new PageResult(page, pageSize, blackList.getTotal(), blackList.getRecords());
    }

    public void deleteBlackList(Long blackUserId) {
        Long userId = UserHolder.getUserId();

        blackListApi.delete(userId,blackUserId);
    }
}
